package GUILayer;

import java.awt.Color;
import java.awt.LayoutManager;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import GUILayer.MainWindow;

@SuppressWarnings("serial")
public class PanelLogin extends JPanel {
	private JTextField userField;
	private JPasswordField passwordField;
	private JFrame frmPareManagement;

	public PanelLogin() {
		setForeground(Color.BLACK);
		setLayout(null);
		
		User();
		JLabel lblBruger = new JLabel("Bruger");
		lblBruger.setBounds(252, 164, 70, 15);
		add(lblBruger);
		
		Password();
		JLabel lblKodeord = new JLabel("Kodeord");
		lblKodeord.setBounds(252, 210, 70, 15);
		add(lblKodeord);
		
		JButton btnLogin = new JButton("Login");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String user = userField.getText();
				char[] password = passwordField.getPassword();
				if (isLoginCorrect(user, password))
				{
					/* insert something if password is correct */
//					roles = 00001;
				} 
				else 
				{
					showAbout();
				}
			}
		});
		btnLogin.setBounds(332, 289, 117, 25);
		add(btnLogin);
		
		
	}
	
	private void User()
	{
		userField = new JTextField();
		userField.setBounds(403, 162, 114, 19);
		add(userField);
		userField.setColumns(10);
	}
	
	private void Password()
	{
		passwordField = new JPasswordField();
		passwordField.setBounds(403, 208, 114, 19);
		add(passwordField);
	}
	
	private static boolean isLoginCorrect(String inputUser, char[] inputPassword) 
	{
		Boolean ok = false;
		String actualUser = "aba";
		char[] actualPassword = {'1','2','3','4'};
		if (inputUser == actualUser)
			ok = true;
		if (inputPassword.length == actualPassword.length)
			ok = true;
		else
			ok = false;
		for (int i = 0; i < inputPassword.length; i++)
			{
				if (inputPassword[i] == actualPassword[i])
					ok = true;
				else
					ok = false;
			}
		return ok;
	}
	
	private void showAbout() {
        JOptionPane.showMessageDialog(frmPareManagement, 
                    "Beklager.\n\n" +
                    "Bruger eller Kodeord er forkert.\n\n",
                    "PareManagement", 
                    JOptionPane.INFORMATION_MESSAGE);
    }

	public PanelLogin(LayoutManager layout) {
		super(layout);
		// TODO Auto-generated constructor stub
	}

	public PanelLogin(boolean isDoubleBuffered) {
		super(isDoubleBuffered);
		// TODO Auto-generated constructor stub
	}

	public PanelLogin(LayoutManager layout, boolean isDoubleBuffered) {
		super(layout, isDoubleBuffered);
		// TODO Auto-generated constructor stub
	}
}
